﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using OrganizedIN.DAL;
using OrganizedIN.BAL;
using OrganizedIN.BussinessServices;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using OrganizedIN.BAL.Common;

namespace OrganizedIN.UI.Media
{
    public partial class SubscribeMedia : System.Web.UI.Page
    {
        BLL objbll = new BLL();
        DateTime dt = DateTime.Now;
        public string strUserName;
        public static string MEDIAID = "";
        public static string path = string.Empty;
        static FileUpload takeMedia1 = new FileUpload();
        MyProfileMedia objMedia = new MyProfileMedia();

        protected void Page_Load(object sender, EventArgs e)
        {
            lblTodayDate.Text = String.Format("{0:D}", dt);
            strUserName = Session["UserID"].ToString();
            getID();
            if (!IsPostBack)
            {
                BindGrid(strUserName);
            }
        }
        public string getID()
        {
            DataSet ds = objbll.Media_FetchMedia_ID();
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0][0].ToString() != "")
                {
                    MEDIAID = ds.Tables[0].Rows[0][0].ToString();
                    int ID = Convert.ToInt32(MEDIAID);
                    ID++;

                    MEDIAID = ID.ToString();
                }
                else
                {
                    MEDIAID = "1";
                }
            }
            return MEDIAID;
        }
        public void BindGrid(string strUserName)
        {
            DataSet ds = objbll.MediaFetch(strUserName);
            if (ds.Tables[0].Rows.Count > 0)
            {
                gvDocList.DataSource = ds.Tables[0];
                gvDocList.DataBind();
            }
            else
            {
                gvDocList.DataSource = null;
                gvDocList.DataBind();
            }
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txt_MediaNAme.Text.Trim() != "")
            {
                lblmsg.Text = "";
                string exe;
                takeMedia1 = FileUpload_Media;
                if (takeMedia1.HasFile)
                {
                    exe = "";
                    exe = System.IO.Path.GetExtension(takeMedia1.FileName).ToLower();
                    string[] ar = { ".avi", ".wmv", ".mpeg", ".mpg", ".mov", ".mp4", ".txt", ".bmp", ".gif", ".jpg", ".jpeg" };
                    for (int i = 0; i < ar.Length; i++)
                    {
                        if (exe == ar[i])
                        {
                            FileUpload_Media.SaveAs(Server.MapPath("~/MediaUpload/" + FileUpload_Media.FileName));
                            path = "~/MediaUpload/" + FileUpload_Media.FileName;
                            break;
                        }
                    }
                }
                else
                {
                    lblmsg.Text = "Please Select A File.";
                }
                objMedia.MediaID = MEDIAID;
                objMedia.Media_Media_Name = txt_MediaNAme.Text.Trim();
                objMedia.MediaSummary = "";
                objMedia.MediaSubscriptionDetail ="";
                objMedia.MediaRating = "1 Star";
                objMedia.MediaStatus = "";
                objMedia.MediaSharerName ="";
                objMedia.MediaUserName = Session["UserID"].ToString();
                objMedia.MediaOfflinePath = path;
                objMedia.Media_OnlineURL_ID = "";
                objMedia.Media_CreateDate = String.Format("{0:d/M/yyyy}", dt);
                objbll.MediaInsert(objMedia);

                txt_MediaNAme.Text = string.Empty;

                getID();
                BindGrid(strUserName);
            }
            else
            {
                lblmsg.Text = "Please Fill Media Name.";
            }
        }
    }
}
